/**
 * Created by vincent on 2017/10/13.
 */
moduleStudyMap.controller("SetupBadgeController", function ($$CambioConfig, $scope, $log, $state, LoadingService, DialogService,
                                                            EnumService, ConstantService, StudyMapBadgeRuleResource, BadgeResource) {
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
    });

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.queryBadge = function () {
        //LoadingService.show();
        BadgeResource.findByKey({}, {key: $scope.badgeKey}, function (badgeResult) {
            $log.debug("勋章匹配结果", badgeResult);
            $scope.badgeResult = badgeResult;
            //LoadingService.hide();
        });
    };

    $scope.setCurrentBadge = function (badge) {
        if (!$scope.currentGroupBadgeRule) {
            $scope.currentGroupBadgeRule = {badge: badge.badgeView};
            $scope.currentGroupBadgeRule.badge.id = badge.badgeView.badgeId;
        } else {
            $scope.currentGroupBadgeRule.badge = badge.badgeView;
            $scope.currentGroupBadgeRule.badge.id = badge.badgeView.badgeId;
        }
    };

    $scope.unActive = function () {
        Alert.confirm("是否确认要执行该操作？", function () {
            //LoadingService.show();
            StudyMapBadgeRuleResource.unActive({}, {id: $scope.currentGroupBadgeRule.id}, function () {
                //$log.debug("勋章匹配结果",badgeResult);
                //$scope.badgeResult = badgeResult;
                //LoadingService.hide();
                Alert.success("操作成功！");
                DialogService.close();
            }, function () {
                //LoadingService.hide();
            });
        });
    }

    $scope.submit = function () {
        if (!StringUtils.isNlOrUndOrEmpty($scope.currentGroupBadgeRule.badge)) {
            //LoadingService.show();
            if (StringUtils.isNlOrUndOrEmpty($scope.currentGroupBadgeRule.id)) {
                StudyMapBadgeRuleResource.add({},
                    {
                        badgeId: $scope.currentGroupBadgeRule.badge.id,
                        studyMapId: $scope.currentGroup.studyMap.id,
                        studyClassGroupId: $scope.currentGroup.id
                    }, function (badgeRuleResult) {
                        $log.debug("章节勋章新增结果", badgeRuleResult);
                        DialogService.close();
                        //LoadingService.hide();
                        Alert.success("设置勋章成功！");
                    }, function () {
                        //LoadingService.hide();
                    });
            } else {
                StudyMapBadgeRuleResource.update({},
                    {
                        id: $scope.currentGroupBadgeRule.id,
                        badgeId: $scope.currentGroupBadgeRule.badge.id,
                        studyMapId: $scope.currentGroup.studyMap.id,
                        studyClassGroupId: $scope.currentGroup.id
                    }, function (badgeRuleResult) {
                        $log.debug("章节勋章更新结果", badgeRuleResult);
                        DialogService.close();
                        //LoadingService.hide();
                        Alert.success("设置勋章成功！");
                    }, function () {
                        //LoadingService.hide();
                    });
            }
        } else {
            Alert.error("勋章信息为空，无法保存！");
        }
    };

});